/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lecture_machine_xml;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;
/**
 *
 * @author phoebus
 */
public class Lecture_machine_xml {
    static org.jdom2.Document document;
    static Element racine;
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
             //On crée une instance de SAXBuilder
     double w = Math.random();
      try
      {
         File fXmlFile = new File("machines.xml");
         
		DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
		Document doc = dBuilder.parse(fXmlFile);
		doc.getDocumentElement().normalize();
 
		System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
                Element racine = doc.getDocumentElement();
               
                NodeList la_machine = racine.getElementsByTagName("MACHINES");
                NodeList nList = racine.getElementsByTagName("SAPHIR");
                // Node la_machine = nList.item(1). 
     
		System.out.println("----------------------------");
               System.out.println( "La machine : "+ racine.getElementsByTagName("*").getLength());
                System.out.println( "Saphir : "+ nList.getLength());
               NodeList nn = ((Element)nList.item(0)).getElementsByTagName("DESCRIPTION");
               System.out.println("nombre de noeuds " +((Element)nn.item(0)).getElementsByTagName("*").getLength());
                System.out.println( "nn "+ nn.getLength());
                
                String type = ((Element)nn.item(0)).getAttribute("type");
                System.out.println( "Type : "+ type);
                
                NodeList version = ((Element)nn.item(0)).getElementsByTagName("VERSION");
                System.out.println( "Version : "+ version.item(0).getTextContent());
                
                NodeList MACHINE_DESCRIPTION = ((Element)nn.item(0)).getElementsByTagName("MACHINE_DESCRIPTION");
                System.out.println( "MACHINE_DESCRIPTION : "+ MACHINE_DESCRIPTION.item(0).getTextContent());
                
                NodeList LEXTENTION_FICHIER = ((Element)nn.item(0)).getElementsByTagName("EXTENTION_FICHIER");
                System.out.println("EXTENTION_FICHIER : "+ LEXTENTION_FICHIER.item(0).getTextContent());
                
                NodeList fin_ligne = ((Element)nn.item(0)).getElementsByTagName("FIN_LIGNE");
                System.out.println("FIN_LIGNE : "+ fin_ligne.item(0).getTextContent());
                
                NodeList fin_fichier = ((Element)nn.item(0)).getElementsByTagName("FIN_FICHIER");
                System.out.println("FIN_FICHIER : " + fin_fichier.item(0).getTextContent());
                
                nn = ((Element)nList.item(0)).getElementsByTagName("PARTNO");
                 type = ((Element)nn.item(0)).getAttribute("GRAMMAIRE");
                System.out.println( "Part no Grammaire : "+ type);
                
                NodeList part_no_format = ((Element)nn.item(0)).getElementsByTagName("FORMAT");
                System.out.println( "Format : "+ part_no_format.item(0).getTextContent());
                
                NodeList part_no_SIGLE = ((Element)nn.item(0)).getElementsByTagName("SIGLE");
                System.out.println( "SIGLE : "+ part_no_SIGLE.item(0).getTextContent());
                
                /*
                 *          Lecture des axes de deplacement
                 *          X Y Z I J K
                 */
                
                nn = ((Element)nList.item(0)).getElementsByTagName("COORDONNEES");
                
                
                // lecture de l axe X
                NodeList X_node = ((Element)nn.item(0)).getElementsByTagName("X");
                NodeList X_node_INSTRUCTION = ((Element)X_node.item(0)).getElementsByTagName("INSTRUCTION");
                
                NodeList X_node_INSTRUCTION_name = ((Element)X_node_INSTRUCTION.item(0)).getElementsByTagName("NAME");
                System.out.println( "Name : "+ X_node_INSTRUCTION_name.item(0).getTextContent());
                
               NodeList X_node_INSTRUCTION_fomat = ((Element)X_node_INSTRUCTION.item(0)).getElementsByTagName("FORMAT");
                System.out.println( "Format : "+ X_node_INSTRUCTION_fomat.item(0).getTextContent());
                
                NodeList X_node_INSTRUCTION_multiplicateur = ((Element)X_node_INSTRUCTION.item(0)).getElementsByTagName("MULTIPLICATEUR");
                System.out.println( "Multiplicateur : "+ X_node_INSTRUCTION_multiplicateur.item(0).getTextContent());
              
                
                //  lecture de l axe Y
                NodeList Y_node = ((Element)nn.item(0)).getElementsByTagName("Y");
                NodeList Y_node_INSTRUCTION = ((Element)Y_node.item(0)).getElementsByTagName("INSTRUCTION");
                
                NodeList Y_node_INSTRUCTION_name = ((Element)Y_node_INSTRUCTION.item(0)).getElementsByTagName("NAME");
                System.out.println( "Name : "+ Y_node_INSTRUCTION_name.item(0).getTextContent());
                
               NodeList Y_node_INSTRUCTION_fomat = ((Element)Y_node_INSTRUCTION.item(0)).getElementsByTagName("FORMAT");
                System.out.println( "Format : "+ Y_node_INSTRUCTION_fomat.item(0).getTextContent());
                
                NodeList Y_node_INSTRUCTION_multiplicateur = ((Element)Y_node_INSTRUCTION.item(0)).getElementsByTagName("MULTIPLICATEUR");
                System.out.println( "Multiplicateur : "+ Y_node_INSTRUCTION_multiplicateur.item(0).getTextContent());
                
                
                //  lecture de l axe Z
                NodeList Z_node = ((Element)nn.item(0)).getElementsByTagName("Z");
                NodeList Z_node_INSTRUCTION = ((Element)Z_node.item(0)).getElementsByTagName("INSTRUCTION");
                
                NodeList Z_node_INSTRUCTION_name = ((Element)Z_node_INSTRUCTION.item(0)).getElementsByTagName("NAME");
                System.out.println( "Name : "+ Z_node_INSTRUCTION_name.item(0).getTextContent());
                
                NodeList Z_node_INSTRUCTION_fomat = ((Element)Z_node_INSTRUCTION.item(0)).getElementsByTagName("FORMAT");
                System.out.println( "Format : "+ Z_node_INSTRUCTION_fomat.item(0).getTextContent());
                
                NodeList Z_node_INSTRUCTION_multiplicateur = ((Element)Z_node_INSTRUCTION.item(0)).getElementsByTagName("MULTIPLICATEUR");
                System.out.println( "Multiplicateur : "+ Z_node_INSTRUCTION_multiplicateur.item(0).getTextContent());
             
                //  lecture de l axe I
                NodeList I_node = ((Element)nn.item(0)).getElementsByTagName("I");
                NodeList I_node_INSTRUCTION = ((Element)I_node.item(0)).getElementsByTagName("INSTRUCTION");
                
                NodeList I_node_INSTRUCTION_name = ((Element)I_node_INSTRUCTION.item(0)).getElementsByTagName("NAME");
                System.out.println( "Name : "+ I_node_INSTRUCTION_name.item(0).getTextContent());
                
                NodeList I_node_INSTRUCTION_fomat = ((Element)I_node_INSTRUCTION.item(0)).getElementsByTagName("FORMAT");
                System.out.println( "Format : "+ I_node_INSTRUCTION_fomat.item(0).getTextContent());
                
                NodeList I_node_INSTRUCTION_multiplicateur = ((Element)I_node_INSTRUCTION.item(0)).getElementsByTagName("MULTIPLICATEUR");
                System.out.println( "Multiplicateur : "+ I_node_INSTRUCTION_multiplicateur.item(0).getTextContent());
                
                //  lecture de l axe J
                NodeList J_node = ((Element)nn.item(0)).getElementsByTagName("J");
                NodeList J_node_INSTRUCTION = ((Element)J_node.item(0)).getElementsByTagName("INSTRUCTION");
                
                NodeList J_node_INSTRUCTION_name = ((Element)J_node_INSTRUCTION.item(0)).getElementsByTagName("NAME");
                System.out.println( "Name : "+ J_node_INSTRUCTION_name.item(0).getTextContent());
                
                NodeList J_node_INSTRUCTION_fomat = ((Element)J_node_INSTRUCTION.item(0)).getElementsByTagName("FORMAT");
                System.out.println( "Format : "+ J_node_INSTRUCTION_fomat.item(0).getTextContent());
                
                NodeList J_node_INSTRUCTION_multiplicateur = ((Element)J_node_INSTRUCTION.item(0)).getElementsByTagName("MULTIPLICATEUR");
                System.out.println( "Multiplicateur : "+ J_node_INSTRUCTION_multiplicateur.item(0).getTextContent());
                
                //  lecture de l axe K
                NodeList K_node = ((Element)nn.item(0)).getElementsByTagName("K");
                NodeList K_node_INSTRUCTION = ((Element)K_node.item(0)).getElementsByTagName("INSTRUCTION");
                
                NodeList K_node_INSTRUCTION_name = ((Element)K_node_INSTRUCTION.item(0)).getElementsByTagName("NAME");
                System.out.println( "Name : "+ K_node_INSTRUCTION_name.item(0).getTextContent());
                
                NodeList K_node_INSTRUCTION_fomat = ((Element)K_node_INSTRUCTION.item(0)).getElementsByTagName("FORMAT");
                System.out.println( "Format : "+ K_node_INSTRUCTION_fomat.item(0).getTextContent());
                
                NodeList K_node_INSTRUCTION_multiplicateur = ((Element)K_node_INSTRUCTION.item(0)).getElementsByTagName("MULTIPLICATEUR");
                System.out.println( "Multiplicateur : "+ K_node_INSTRUCTION_multiplicateur.item(0).getTextContent());
                
                //Spindle
                 NodeList nspindle = ((Element)nList.item(0)).getElementsByTagName("SPINDL");
                 
                 //  CCW
                 NodeList CCW_node = ((Element)nspindle.item(0)).getElementsByTagName("CCW");
                 System.out.println(((Element)CCW_node.item(0)).getAttribute("GRAMMAIRE"));
                 NodeList CCW_node_instruction = ((Element)CCW_node.item(0)).getElementsByTagName("INSTRUCTION");
                System.out.println( "Instruction : "+ CCW_node_instruction.item(0).getTextContent());
                
                 //  CW
                 NodeList CW_node = ((Element)nspindle.item(0)).getElementsByTagName("CW");
                 System.out.println(((Element)CW_node.item(0)).getAttribute("GRAMMAIRE"));
                 NodeList CW_node_instruction = ((Element)CW_node.item(0)).getElementsByTagName("INSTRUCTION");
                 System.out.println( "Instruction : "+ CW_node_instruction.item(0).getTextContent());
                 
                  //  OFF
                 NodeList OFF_node = ((Element)nspindle.item(0)).getElementsByTagName("OFF");
                 System.out.println(((Element)OFF_node.item(0)).getAttribute("GRAMMAIRE"));
                 NodeList OFF_node_instruction = ((Element)OFF_node.item(0)).getElementsByTagName("INSTRUCTION");
                 System.out.println( "Instruction : "+ OFF_node_instruction.item(0).getTextContent());
                 
                  //  INSTRUCTION
                 NodeList INSTRUCTION_node = ((Element)nspindle.item(0)).getElementsByTagName("INSTRUCTIONS");
                 
                 NodeList INSTRUCTION_node_name = ((Element)INSTRUCTION_node.item(0)).getElementsByTagName("NAME");
                 System.out.println( "Instruction name: "+ INSTRUCTION_node_name.item(0).getTextContent());
                 
                 NodeList INSTRUCTION_node_format = ((Element)INSTRUCTION_node.item(0)).getElementsByTagName("FORMAT");
                 System.out.println( "Instruction format: "+ INSTRUCTION_node_format.item(0).getTextContent());
                 
                  NodeList UNITES_node = ((Element)nspindle.item(0)).getElementsByTagName("UNITES");
                 NodeList UNITES_node_rpm = ((Element)UNITES_node.item(0)).getElementsByTagName("RPM");
                 System.out.println( "Unites RPM: "+ UNITES_node_rpm.item(0).getTextContent());
                 
                 NodeList UNITES_node_mpm = ((Element)UNITES_node.item(0)).getElementsByTagName("MPM");
                 System.out.println( "Unites MPM: "+ UNITES_node_mpm.item(0).getTextContent());
                 
                 // load tool
                 NodeList LOAD_TOOL_node = ((Element)nList.item(0)).getElementsByTagName("LOAD_TOOL");
                 System.out.println("Grammaire load tool "+((Element)LOAD_TOOL_node.item(0)).getAttribute("GRAMMAIRE"));
                 NodeList LOAD_TOOL_TOOL_CHANGE = ((Element)LOAD_TOOL_node.item(0)).getElementsByTagName("TOOL_CHANGE");
                 //System.out.println( "load tool tool change : "+ LOAD_TOOL_TOOL_CHANGE.item(0).getTextContent());
                 NodeList LOAD_TOOL_TOOL_CHANGE_INSTRUCTION = ((Element)LOAD_TOOL_node.item(0)).getElementsByTagName("INSTRUCTION");
                 System.out.println( "load tool instruction: "+ LOAD_TOOL_TOOL_CHANGE_INSTRUCTION.item(0).getTextContent());
                 NodeList LOAD_TOOL_INSTRUCTIONS = ((Element)LOAD_TOOL_node.item(0)).getElementsByTagName("INSTRUCTIONS");
                 NodeList LOAD_TOOL_TOOL_CHANGE_INSTRUCTION_NAME = ((Element)LOAD_TOOL_node.item(0)).getElementsByTagName("NAME");
                 System.out.println( "load tool name: "+ LOAD_TOOL_TOOL_CHANGE_INSTRUCTION_NAME.item(0).getTextContent());
                 NodeList LOAD_TOOL_TOOL_CHANGE_INSTRUCTION_FORMAT = ((Element)LOAD_TOOL_node.item(0)).getElementsByTagName("FORMAT");
                 System.out.println( "load tool format: "+ LOAD_TOOL_TOOL_CHANGE_INSTRUCTION_FORMAT.item(0).getTextContent());
                 
                 
                System.out.println("fin");
                
      }
      catch(Exception e){
       System.out.println("erreur");
       System.out.println(e.getMessage());
      }

    
   }
        

}